"""Schemas for new chat session & message system (independent)."""

from __future__ import annotations

from datetime import datetime
from typing import Optional, List, Any, Dict
from pydantic import BaseModel, Field


class ChatSessionCreate(BaseModel):
    title: Optional[str] = None
    system_prompt: Optional[str] = None
    model_name: Optional[str] = None
    book_id: Optional[int] = None
    meta: Optional[Dict[str, Any]] = None


class ChatSessionOut(BaseModel):
    id: int
    title: Optional[str]
    model_name: Optional[str]
    book_id: Optional[int]
    message_count: int
    pinned: bool
    archived: bool
    status: str
    last_message_at: datetime
    created_at: datetime
    updated_at: datetime
    meta: Optional[Dict[str, Any]] = None

    class Config:
        from_attributes = True


class ChatMessageCreate(BaseModel):
    content: str = Field(min_length=1, max_length=4000)
    model_name: Optional[str] = None
    parent_message_id: Optional[int] = None
    stream: bool = True
    mode: Optional[str] = None


class ChatMessageOut(BaseModel):
    id: int
    session_id: int
    role: str
    content: str
    seq: int
    is_final: bool
    created_at: datetime
    model_name: Optional[str] = None
    meta: Optional[Dict[str, Any]] = None

    class Config:
        from_attributes = True


class ChatSessionList(BaseModel):
    sessions: List[ChatSessionOut]
    total: int


class ChatMessageList(BaseModel):
    messages: List[ChatMessageOut]
    total: int


class ChatSessionUpdate(BaseModel):
    """Update fields for a chat session.

    Title can be changed, and meta can carry lightweight
    per-session settings such as role mode.
    """

    title: Optional[str] = Field(None, max_length=60)
    meta: Optional[Dict[str, Any]] = None
